#include <REGX52.H>
#define AR P1_0
#define AY P1_1
#define AG P1_2
#define BR P1_3
#define BY P1_4
#define BG P1_5

char count,second=10;
char AF=1,BF=0;
void Delay(unsigned int xms)
{
	unsigned char i, j;
	while(xms--)
	{
		i = 2;
		j = 239;
		do
		{
			while (--j);
		} while (--i);
	}
}
void timerInit()
{
	 TMOD|=0x01;
	 TH0=(65536-50000)/256;
	 TL0=(65536-50000)%256;
	 ET0=1;
	 TR0=1;
	 EA=1;
}

void ITR_Init()
{
	IT0=0;
	EX0=1;
	PX0=1;
}

void SG(int a,int b)
{
	unsigned char Tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x40};
  unsigned char Count[]={0x00,0x01,0x02};
	P2=~Count[a];
	P0=~Tab[b];
	Delay(6);
}
void main()
{
	timerInit();
	ITR_Init();
	while(1)
	{
		SG(1,second/10);
		SG(2,second%10);
   
	}
}

void Timer() interrupt 1
{
	 TH0=(65536-50000)/256;
	 TL0=(65536-50000)%256;
	 count++;
	if(count==20)
	{
		second--;
		count=0;
	}
	if(second<0)
	{
		AF=~AF;
		BF=~BF;
		second=10;
	}
	if(second>3&&AF)
	{
		AR=0;
		AY=0;
		BY=0;
		BG=0;
		AG=1;
		BR=1;
	}
	if(second<3&&AF)
	{
		AR=0;
		BY=0;
		BG=0;
		AG=0;
		AY=1;
		BR=1;
//		P1=0xFF;
//		AY=0;
//		BR=0;
	}
	if(second>3&&BF)
	{

		AY=0;
		BY=0;

		AG=0;
		BR=0;
		BG=1;
		AR=1;
//		P1=0xFF;
//		BG=0;
//		AR=0;
	}
	if(second<3&&BF)
	{
		
		AY=0;
		BG=0;
		AG=0;
		BR=0;
		BY=1;
		AR=1;
//		P1=0xFF;
//		BY=0;
//		AR=0;
	}

}
void IRT0() interrupt 0    //
{
	  P2=0xFF;
		AR=1;
		AY=0;
		BY=0;
		BG=0;
		AG=0;
		BR=1;
}
